package br.com.firula.web.filter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

import org.displaytag.tags.TableTagParameters;
import org.displaytag.util.ParamEncoder;

import br.com.firula.to.BaseTO;

import com.opensymphony.xwork2.ActionContext;

public class Filtro {

	private HashMap<String, String> data = new HashMap<String, String>();
	private Integer page = 1;
	private Integer pageSize = 20;
	private int maxResults;
	private Class clazz;
	private BaseTO[] example;
	private ActionContext actionContext;
	private String order;
	private String busca;
	private Date dataCriacaoInicio;
	private Date dataCriacaoTermino;
	private Date dataChamadoInicio;
	private Date dataChamadoTermino;
	private boolean export = false;
	
	
	public boolean isExport() {
		return export;
	}
	public void setExport(boolean export) {
		this.export = export;
	}
	public Filtro(ActionContext context) {
		setActionContext(context);
	}
	public Filtro() {
	}
	public HashMap<String, String> getData() {
		return data;
	}
	public int getMaxResults() {
		return export ? 0 : maxResults;
	}
	public Integer getPage() {
		return export ? 0 : page;
	}
	public Integer getPageSize() {
		return export ? 0 : pageSize;
	}
	public void setData(HashMap<String, String> data) {
		this.data = data;
	}
	public void setMaxResults(int maxResults) {
		this.maxResults = maxResults;
	}
	public void setPage(Integer page) {
		this.page = page;
	}
	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}
	public Class getClazz() {
		return clazz;
	}
	public void setClazz(Class clazz) {
		this.clazz = clazz;
	}
	public BaseTO[] getExample() {
		return example;
	}
	public void setExample(BaseTO example) {
		this.example = new BaseTO[]{example};
	}
	public int getFirst() {
		return export ? 0 : (page - 1)  * pageSize ;
	}
	public void setActionContext(ActionContext context) {
		this.actionContext = context;
		try {
			Object param = actionContext.getParameters().get(new ParamEncoder("basesList").encodeParameterName(TableTagParameters.PARAMETER_PAGE));
			String paramString = ((String[])param)[0];
			page = Integer.parseInt(paramString);
		} catch (Exception e) {
		}
		
		Object param2 = actionContext.getParameters().get( new ParamEncoder("basesList").encodeParameterName(TableTagParameters.PARAMETER_EXPORTTYPE));
		export = param2 != null;
		
		
		for (Object key : actionContext.getParameters().keySet()) {
			if (key instanceof String) {
				Object value = actionContext.getParameters().get(key);
				if (value instanceof String) {
					data.put((String)key, (String)value);
				} else {
					if (value instanceof String[]) {
						data.put((String)key, ((String[])value)[0]);
					}
				}
			}
		}
	}
	public String getOrder() {
		return order;
	}
	public void setOrder(String order) {
		this.order = order;
	}
	public void setExample(BaseTO[] example) {
		this.example = example;
	}
	public String getBusca() {
		return busca;
	}
	public void setBusca(String busca) {
		this.busca = busca;
	}	
	public void createBlankExample(){
		try {
			this.example = new BaseTO[]{(BaseTO)getClazz().getConstructor(null).newInstance(null)};
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public Date getDataCriacaoInicio() {
		return dataCriacaoInicio;
	}
	public void setDataCriacaoInicio(Date dataCriacaoInicio) {
		this.dataCriacaoInicio = dataCriacaoInicio;
	}
	public Date getDataCriacaoTermino() {
		return dataCriacaoTermino;
	}
	public void setDataCriacaoTermino(Date dataCriacaoTermino) {
		this.dataCriacaoTermino = dataCriacaoTermino;
	}
	public void setDataCriacaoInicioString(String s){
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
		try {
			this.setDataCriacaoInicio(sdf.parse(s));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
		}
	}
	public void setDataCriacaoTerminoString(String s){
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
		try {
			this.setDataCriacaoTermino(sdf.parse(s));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
		}
	}
	public String getDataCriacaoInicioString(){
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
		return (dataCriacaoInicio != null && !"".equals(dataCriacaoInicio) ? sdf.format(dataCriacaoInicio) : null);
	}
	public String getDataCriacaoTerminoString(){
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
		return (dataCriacaoTermino != null && !"".equals(dataCriacaoTermino) ? sdf.format(dataCriacaoTermino) : null);
	}
	
	public Date getDataChamadoInicio() {
		return dataChamadoInicio;
	}
	public void setDataChamadoInicio(Date dataChamadoInicio) {
		this.dataChamadoInicio = dataChamadoInicio;
	}
	public Date getDataChamadoTermino() {
		return dataChamadoTermino;
	}
	public void setDataChamadoTermino(Date dataChamadoTermino) {
		this.dataChamadoTermino = dataChamadoTermino;
	}
	
	public void setDataChamadoInicioString(String s){
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
		try {
			this.setDataChamadoInicio(sdf.parse(s));
		} catch (ParseException e) {
		}
	}
	public void setDataChamadoTerminoString(String s){
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
		try {
			this.setDataChamadoTermino(sdf.parse(s));
		} catch (ParseException e) {
		}
	}
		
	public String getDataChamadoInicioString(){
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
		return (dataChamadoInicio != null && !"".equals(dataChamadoInicio) ? sdf.format(dataChamadoInicio) : null);
	}
	public String getDataChamadoTerminoString(){
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
		return (dataChamadoTermino != null && !"".equals(dataChamadoTermino) ? sdf.format(dataChamadoTermino) : null);
	}
	
}
